//
//  HeaderView.m
//  WXMovie
//
//  Created by HZApple on 16/4/27.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "HeaderView.h"
#import <MediaPlayer/MediaPlayer.h>

@interface HeaderView () {
    
    UIImageView *imageView;
    UILabel *titleLabel;
    UILabel *directorsLabel;
    UILabel *actorsLabel;
    UILabel *typeLabel;
    UILabel *releaseLabel;
    NSDictionary *dic;
    NSArray *array;
}

@end

@implementation HeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self =[super initWithFrame:frame]) {
        [self _loadData];
        [self _createUI];
        
    }
    return self;
}

- (void)_createUI {
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 80, 100)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"image"]]];
    
    [self addSubview:imageView];
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 240, 30)];
    titleLabel.font = [UIFont systemFontOfSize:24];
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.text = dic[@"titleCn"];
    [self addSubview:titleLabel];
    directorsLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 35, 240, 25)];
    directorsLabel.font = [UIFont systemFontOfSize:14];
    directorsLabel.textColor = [UIColor whiteColor];
    [self addSubview:directorsLabel];
    actorsLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 55, 240, 25)];
    actorsLabel.textColor = [UIColor whiteColor];
    actorsLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:actorsLabel];
    typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 75, 240, 25)];
    typeLabel.textColor = [UIColor whiteColor];
    typeLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:typeLabel];
    releaseLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 95, 240, 25)];
    releaseLabel.font = [UIFont systemFontOfSize:14];
    releaseLabel.textColor = [UIColor whiteColor];
    [self addSubview:releaseLabel];
    
    
    NSMutableString *directorsString = [NSMutableString stringWithString:@"导演："];
    for (NSString *str in dic[@"directors"]) {
        [directorsString appendString:[NSString stringWithFormat:@"%@、",str]];
    }
    NSRange range = NSMakeRange(directorsString.length - 1, 1);
    [directorsString replaceCharactersInRange:range withString:@""];
    directorsLabel.text = directorsString;
    
    NSMutableString *actorsString = [NSMutableString stringWithString:@"主演："];
    for (NSString *str in dic[@"actors"]) {
        [actorsString appendString:[NSString stringWithFormat:@"%@、",str]];
    }
    range = NSMakeRange(actorsString.length - 1, 1);
    [actorsString replaceCharactersInRange:range withString:@""];
    actorsLabel.text = actorsString;
    
    
    NSMutableString *typeString = [NSMutableString stringWithString:@"类型："];
    for (NSString *str in dic[@"type"]) {
        [typeString appendString:[NSString stringWithFormat:@"%@、",str]];
    }
    range = NSMakeRange(typeString.length - 1, 1);
    [typeString replaceCharactersInRange:range withString:@""];
    typeLabel.text = typeString;
    
    NSMutableString *releaseString = [NSMutableString string];
    NSDictionary *dic2 = dic[@"release"];
    [releaseString appendString:dic2[@"location"]];
    [releaseString appendString:dic2[@"date"]];
    releaseLabel.text = releaseString;

    
   
    for (int i = 0; i < 4; i++) {
        UIImageView *picture = [[UIImageView alloc] initWithFrame:CGRectMake(15 + 80 * i, 120, 80, 70)];
        picture.tag = i + 50;
        picture.layer.cornerRadius = 5;
        picture.layer.masksToBounds = YES;
        picture.userInteractionEnabled = YES;
        NSDictionary *dic3 = array[i];
   
        [picture sd_setImageWithURL:[NSURL URLWithString:dic3[@"image"]]];

        [self addSubview:picture];

    }
    
}


- (void)_loadData{
    dic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"movie_detail" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil];
    
    array = dic[@"videos"];
}


@end
